package com.zhouxiaoge.状态模式.电梯问题.one;

/**
 * @author 周小哥 gqzmy@163.com 2023/2/27 15:03
 */

public class LiftImpl implements Lift {

    private int state;

    @Override
    public void setState(int state) {
        this.state = state;
    }

    @Override
    public void open() {
        switch (this.state) {
            case OPENING_STATE:
                System.out.println("电梯门已是打开状态");
                break;
            case CLOSING_STATE:
                this.openWithoutLogic();
                break;
            case RUNNING_STATE:
                System.out.println("电梯运行期间，不允许打开门");
                break;
            case STOPPING_STATE:
                this.openWithoutLogic();
                this.setState(OPENING_STATE);
                break;
        }
    }

    @Override
    public void close() {
        switch (this.state) {
            case OPENING_STATE:
                this.closeWithoutLogic();
                this.setState(CLOSING_STATE);
                break;
            case CLOSING_STATE:
                System.out.println("电梯门已是关闭状态");
                break;
            case RUNNING_STATE:
                System.out.println("电梯运行期间，门已是关闭状态");
                break;
            case STOPPING_STATE:
                System.out.println("电梯停止期间，门已是关闭状态");
                break;
        }
    }

    @Override
    public void run() {
        switch (this.state) {
            case OPENING_STATE:
                System.out.println("电梯门为敞开状态，不允许运行");
                break;
            case CLOSING_STATE:
            case STOPPING_STATE:
                this.runWithoutLogic();
                this.setState(RUNNING_STATE);
                break;
            case RUNNING_STATE:
                System.out.println("电梯已是运行状态");
                break;
        }
    }

    @Override
    public void stop() {
        switch (this.state) {
            case OPENING_STATE:
                System.out.println("电梯门为敞开状态，说明电梯已经停止");
                break;
            case CLOSING_STATE:
            case RUNNING_STATE:
                this.stopWithoutLogic();
                this.setState(STOPPING_STATE);
                break;
            case STOPPING_STATE:
                System.out.println("电梯已是停止状态");
                break;
        }
    }


    // 纯粹的电梯关门，不考虑实际的逻辑
    private void closeWithoutLogic() {
        System.out.println("电梯门关闭...");
    }

    // 纯粹的店门开，不考虑任何条件
    private void openWithoutLogic() {
        System.out.println("电梯门开启...");
    }

    // 纯粹的运行，不考虑其他条件
    private void runWithoutLogic() {
        System.out.println("电梯上下跑起来...");
    }

    // 单纯的停止，不考虑其他条件
    private void stopWithoutLogic() {
        System.out.println("电梯停止了...");
    }
}
